$(function() {
	$("#createUserInfoSubmitBtn").bind("click", function() {
		$.ajax({
			url:"./createUserInfo.action",
			type:'post',
			data:$("#createUserInfoForm").serialize(),
			success:function(data, textStatus, jqXHR){
				if(!data.isSuccess){
					showErrorTip(data.errors);
				}
				console.log(data);
			}
		});
		event.preventDefault();
		return false;
	});
	function showErrorTip(errorObj){
		var formObj=$("#createUserInfoForm");
		$.each(errorObj,function(i,ele){
			var appendContent="<p class=\"bg-danger colin-error-tip\">"+ele+"<button type=\"button\" class=\"close\" aria-label=\"Close\" ><span aria-hidden=\"true\">×</span></button></p>"
			formObj.find("input[name='"+i+"']").siblings("p").remove();
			formObj.find("input[name='"+i+"']").after(appendContent);
			listenCloseBtnTip();
		});
	};
	function listenCloseBtnTip(){
		$("button[class='close']").bind("click",function(e){
			$(this).parent("p").remove();
		})
	}
	$("#findUserInfoBtn").bind("click",function(){
		$.ajax({
			url:'./findUserInfo.action',
			type:'post',
			data:$("#findUserInfoForm").serialize(),
			success:function(data,textStatus){
				var tableObj=$("#findUserInfoTable");
				tableObj.html("");
				if(data!=""&&data.length>0){
					var searchResultTemplate="";
					$.each(data,function(i,user){
						searchResultTemplate+="<tr><td class='text-center'>"+user.username+"</td><td class='text-center'>"+user.birthday+"</td><td class='text-center'>"+user.email+"</td><td class='text-center'>"+user.sex+"</td></tr>";
					});
						
					tableObj.html(searchResultTemplate);
				}else{
					alert("查无此数据");
				}
			}
		});
		event.preventDefault();
		return false;
	});
	$("#findUsernameBtn").on("click",function(e){
		$.post("findUsername.action",$("#findUsernameForm").serialize(),function(data){
			var content="";
			$.each(data,function(i,e){
				content+=e;
			});
			$("#findUsernamePanel").html(content);
		});
		e.preventDefault();
		return false;
	});
});